/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.base

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * Author: czmc  Email: @tuwan21.com
 * Version:	1.0  Date:3/3/2018
 */

fun <T> Observable<T>.io_main(): Observable<T> {
    return subscribeOn(Schedulers.io())
            .unsubscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
}

fun Context.showTipDialog(t: String, c: String,confimL:View.OnClickListener) {
    val dialog = me.drakeet.materialdialog.MaterialDialog(this)
    dialog.setTitle(t)
            .setMessage(c)
            .setPositiveButton("确定", {
                v-> confimL.onClick(v)
                dialog.dismiss()
            })
    dialog.show()
}


fun Context.showConfirmDialog(t: String, c: String,confimL:View.OnClickListener) {
    val dialog = me.drakeet.materialdialog.MaterialDialog(this)
    dialog.setTitle(t)
            .setMessage(c)
            .setPositiveButton("确定", {
                v->
                confimL.onClick(v)
                dialog.dismiss()
            })
            .setNegativeButton("取消",{dialog.dismiss()})
    dialog.show()
}
fun RecyclerView.setOnLoadMore(callback:()->Unit){

    addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)
        }

        override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            if ( (computeVerticalScrollExtent() + computeVerticalScrollOffset() >= computeVerticalScrollRange()) ) {
                callback.invoke()
            }
        }
    })
}
